<?php

require_once BaseUtil::getModulePath("dbgrid") . '/DBGrid.php';
require_once BaseUtil::getModulePath("dbgrid") . '/DBGridConstant.php';
require_once BaseUtil::getModulePath("base") . '/services/BaseService.php';
require_once BaseUtil::getModulePath("config") . '/models/EmailTemplateDao.php';

class EmailTemplateService extends BaseService {
	
	private $emailtemplateDao;
	
	public function __construct() {
		parent::__construct();
		$this->emailtemplateDao = new EmailTemplateDao();
		$this->emailtemplateDao->setDb($this->db);
	}
	
	public function getTemplate($view,$form) {
		$dbGrid = new DBGrid($view);
		$dbGrid->setPost($form);
		$dbGrid->setModule("/config/emailtemplate");
		$dbGrid->addValueFilter("is_delete",0);
		$dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,'subject',$this->_translate->translate("subject"));
		$dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,'tag',$this->_translate->translate("tag"));
		$dbGrid->addFetchTable("email_templates","*");
		$dbGrid->addFetchColumn("subject",$this->_translate->translate("subject"),"edit");
		$dbGrid->addFetchColumn("sender",$this->_translate->translate("sender"));
		$dbGrid->addFetchColumn("tag",$this->_translate->translate("tag"));
		$dbGrid->addFetchColumn("content",$this->_translate->translate("content"));
		$dbGrid->addFetchColumn("created",$this->_translate->translate("createDate"));
		//module action
		$dbGrid->addAAModuleAction("emailtemplate","edit",$this->_translate->translate("editBtn"),"edit");
		$dbGrid->addAAModuleAction("emailtemplate","delete",$this->_translate->translate("delBtn"),"deletesome");
		$dbGrid->addAAModuleAction("emailtemplate","add",$this->_translate->translate("addBtn"),"add");
		$dbGrid->setPageTitle($this->_translate->translate("searchTitle"));
		return $dbGrid->render();
	}
	
	public function addTemplate($form) {
		$template = array();
		$template["subject"] = $form->getValue("subject");
		$template["sender"] = $form->getValue("sender");
		$template["tag"] = $form->getValue("tag");
		$template["content"] = $form->getValue("content");
		$this->emailtemplateDao->insertData($template);
	}
	
	public function editTemplate($form) {		
		$template = array();
		$template["subject"] = $form->getValue("subject");
		$template["sender"] = $form->getValue("sender");
		$template["tag"] = $form->getValue("tag");
		$template["content"] = $form->getValue("content");
		$this->emailtemplateDao->updateTemplate($template,$form->getValue("id"));
	}
	
	public function getTemplateById($id) {
		return $this->emailtemplateDao->getByCol("id",$id);	   
	}
	
	public function deleteTemplate($ids) {
		foreach($ids as $id){
			if(!empty($id)){
			     $this->emailtemplateDao->deleteTemplate($id);
			}
		}
	}
	
	public function delete($ids) {
		if(!empty($ids)){
			$this->emailtemplateDao->deleteTemplate($ids);
		}
	}
}